﻿package com.admin.controller;

import java.sql.Timestamp;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.common.BaseController;
import com.common.CommonConstraint;
import com.entity.ProductImage;
import com.entity.Production;
import com.my.MyTools;
import com.service.ProductImageService;
import com.util.FileUtils;

@Controller
@RequestMapping("/admin/product")
public class AdminProductImageController extends BaseController {
	@Autowired
	private ProductImageService productImageService;
	
	@RequestMapping("/list-image")
	public String listPics(HttpServletRequest request, HttpServletResponse response){
		String proId = request.getParameter("id");
		if(StringUtils.isEmpty(proId)){
			this.outJson("[]", response);
		}else{
			List<ProductImage> list = this.productImageService.findByProduct(proId);
			JSONArray array = new JSONArray();
			for(ProductImage image:list){
				JSONObject obj = new JSONObject();
				obj.put("id", image.getId());
				obj.put("image_sort", image.getImageSort());
				obj.put("add_time", image.getAddTime());
				obj.put("image_editor", image.getImageEditor());
				obj.put("is_rotate", image.getIsRotate());
				array.add(obj);
			}
			this.outJson(array.toJSONString(), response);
		}
		return null;
	}
	
	@RequestMapping("/saveimage")
	public ModelAndView saveImage(HttpServletRequest request, HttpServletResponse response){
		boolean result = false;
		String msg = "";
		try{
			MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
			Map<String, MultipartFile> files = multipartRequest.getFileMap();
			Iterator<String> fileNames = multipartRequest.getFileNames();
			String productId = request.getParameter("p");
			if(productId != null){
				while(fileNames.hasNext()){
					String fileName = fileNames.next();
					System.out.println("文件名为：" + fileName);
					if(!MyTools.isEmpty(fileName)){
						CommonsMultipartFile file = (CommonsMultipartFile) files.get(fileName);
						System.out.println("原文件路径为：" + file.getOriginalFilename());
						String filePath = FileUtils.saveFile(file, request.getSession().getServletContext().getRealPath("/"), CommonConstraint.UPLOAD_PATH);
						System.out.println("保存文件路径为：" + filePath);
						ProductImage image = new ProductImage();
						image.setAddTime(new Timestamp(System.currentTimeMillis()));
						image.setIsRotate(false);
						image.setProductImage(filePath);
						Production pro = new Production();
						pro.setId(Integer.parseInt(productId));
						image.setProduction(pro);
						productImageService.save(image);
					}
				}
				result = true;
				msg = "保存成功！";
			}else{
				msg = "请先选择产品！";
			}
		} catch (ClassCastException e){
			msg = "请上传文件！";
		}
		outJson("{success:" + result + ",msg:\"" + msg + "\"}", response);
		return null;
	}
	
	@RequestMapping("/image-form")
	public String imageForm(){
		return "admin.product.image.edit";
	}
}
